package router

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"HeadLineNews/settings"
	"HeadLineNews/utils/middlewares"
	"log"
)

// activeRouter 激活总的路由服务
func activeRouter(engine *gin.Engine) {
	r := newRouter(engine.Group("api/"))
	activeManageAdminServer(r)      // 管理员
	activeManageAuthorServer(r)     // 新闻作者
	activeManageCategoryServer(r)   // 新闻分类
	activeManageNewsServer(r)       // 新闻
	activeManagePermissionServer(r) // 权限控制

	activeAppUserServer(r)        // 用户登录&注册
	activeAppUserNewsServer(r)    // 用户查看新闻
	activeAppUserCommentServer(r) // 用户评论
	activeAppUserVoteServer(r)    // 用户点赞
}

// StartServer 开启服务
func StartServer() {
	engine := gin.New()
	if settings.Settings.Mode == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode)
		// 使用恢复中间件...
		engine.Use(middlewares.GinRecovery) // 开发阶段可以 panic 方便定位问题
	}
	activeRouter(engine) // 激活路由
	//if err := storeRouter(); err != nil {  // 存储路由
	//	return
	//}
	if err := engine.Run(fmt.Sprintf("%s:%s",
		settings.Settings.Host,
		settings.Settings.Port,
	)); err != nil {
		log.Fatalf("start server failed. error: %v", err)
	}
}
